package com.sfpay.coreplatform.order.service.composite.impl;

import org.springframework.stereotype.Service;

import com.sfpay.coreplatform.order.common.OrderResponseCode;
import com.sfpay.coreplatform.order.service.composite.common.PayContext;
import com.sfpay.coreplatform.order.valueobject.dto.InnerResponse;
import com.sfpay.coreplatform.order.valueobject.dto.PayRequest;
import com.sfpay.coreplatform.order.valueobject.dto.Response;

@Service
public class CheckRepeatHandler extends AbstractHandler {

	@Override
	public Response Handler(PayRequest request, PayContext payContext) {
		Response response = new Response();
		response.setReturnCode(OrderResponseCode.ORDER_RESPONSE_SUCESS);
		if (codeMap.get(request.getOutTradeNo()) != null) {

			response.setReturnCode(OrderResponseCode.ORDER_RESPONSE_SAME_ORDER);
			response.setReturnMsg("method:checkRepeatHandel the same order");
			return response;
		}
		synchronized (codeMap) {
			if (codeMap.get(request.getOutTradeNo()) != null) {
				response.setReturnCode(OrderResponseCode.ORDER_RESPONSE_SAME_ORDER);
				response.setReturnMsg("method:checkRepeatHandel the same order");
				return response;
			}
			codeMap.put(request.getOutTradeNo(), request.getOutTradeNo());
			response.setReturnCode(OrderResponseCode.ORDER_RESPONSE_SUCESS);
		}
		return response;
	}

	/**
	 * 去掉code
	 * @param code
	 */
	public void removeOverCodeHandler(String code) {

		codeMap.remove(code);

	}

	@Override
	public String getHandlerName() {
		 
		return "CheckRepeatHandler";
	}

}
